<?php
/**
 * 看宁波微信档期管理模块微站定义
 *
 * @author ying
 * @url http://bbs.we7.cc/
 */
defined('IN_IA') or exit('Access Denied');

class Knbsys_scheduleModuleSite extends WeModuleSite {
	private $tablename_schedule				= 'knbsys_schedule';
	private $tablename_record				= 'knbsys_schedule_record';

	//档期管理
	public function doWebAcmanage() {
		global $_GPC, $_W;

		$op = $_GPC['op'];
		load()->func('tpl');
		$actime = $_GPC['actime'];
		$rank	= $_GPC['rank'];

        $profile =  pdo_fetch("SELECT * FROM " . tablename('users_profile') ." where uid = :uid",array(':uid'=>$_W['uid']));
        
        //对接人信息
        $cfg = $this->module['config'];
		$content_people		= $cfg['content_people'];
		$business_people	= $cfg['business_people'];
		$content_people_array	= explode(',', $content_people);
		$business_people_array	= explode(',', $business_people);

        //操作用户
        $user_admin         = $cfg['user_admin'];
        $user_admin_array	= explode(',', $user_admin);

        $just_do = 2;
        if(in_array($_W['user']['username'],$user_admin_array)){
            $just_do = 1;
        }

		//公用数组
		$typearray	= array('1'=>'付费','2'=>'资源','3'=>'原创','4'=>'资源+付费');
		$rankarray	= array('1'=>'头条','2'=>'次一','3'=>'次二','4'=>'次三','5'=>'次四');
		$doarray	= array('1'=>'添加','2'=>'修改','3'=>'删除');
		$insert_record_array = array(
			'uid'		=> $_W['uid'],
			'username'	=> $_W['username'],
			'realname'	=> $profile['realname'],
			'addtime'	=> $_W['timestamp'],
		);

		if($op == 'edit') {
			$id 	= $_GPC['id'];
			if (checksubmit('submit')){
				$rows = $_GPC['rows'];
				$rows['actime'] = strtotime($actime);

				if($id) {
					$rank = $rows['rank'];
					$actime = $rows['actime'];
					$row =  pdo_fetch("SELECT * FROM " . tablename($this->tablename_schedule) ." where actime=:actime and rank=:rank and id!=:id",array(':actime'=>$actime,':rank'=>$rank,':id'=>$id));
					if($row) {
						message('该日档期已存在', $this->createWebUrl('Acmanage'), error);
					}

					$rows['altertime'] = $_W['timestamp'];
					$update = pdo_update($this->tablename_schedule, $rows, array('id'=>$id));
					if ($update){
						$insert_record_array['type'] = 2;
						$insert_record_array['actitle']=$rows['title'];

						pdo_insert($this->tablename_record, $insert_record_array);
						message('数据更新成功.',$this->createWebUrl('Acmanage'),'success');
					} else {
						message('数据更新失败.',$this->createWebUrl('Acmanage'),'error');
					}
				} else {
					$rank = $rows['rank'];
					$actime = $rows['actime'];
					$row =  pdo_fetch("SELECT * FROM " . tablename($this->tablename_schedule) ." where actime=:actime and rank=:rank",array(':actime'=>$actime,':rank'=>$rank));
					if($row) {
						message('该日档期已存在', $this->createWebUrl('Acmanage'), error);
					}

					$rows['addtime'] = $_W['timestamp'];
					$rows['uid'] = $_W['uid'];
					$rows['realname'] = $profile['realname'];

					$insert = pdo_insert($this->tablename_schedule, $rows);

					if ($insert) {
						$insert_record_array['type']=1;
						$insert_record_array['actitle']=$rows['title'];
						pdo_insert($this->tablename_record, $insert_record_array);

						message('数据添加成功.',$this->createWebUrl('Acmanage'),'success');
					} else {
						message('数据添加失败.',$this->createWebUrl('Acmanage'),'error');
					}
				}
			}
			$list = pdo_fetch("SELECT * FROM " . tablename($this->tablename_schedule) ." where id=:id",array(':id'=>$id));
			if($list) {
				$actime = $list['actime'];
				$rank = $list['rank'];
				$row['type'] = $list['type'];
			}
			if(!$actime) {
				$actime = $_W['timestamp'];
			}

		} else if($op == 'detail'){
			$row =  pdo_fetch("SELECT * FROM " . tablename($this->tablename_schedule) ." where actime=:actime and rank=:rank",array(':actime'=>$actime,':rank'=>$rank));
		} else if($op == 'delete'){
			$id = $_GPC['id'];
			$title = pdo_fetchcolumn("SELECT title FROM " . tablename($this->tablename_schedule) ." where id=:id",array(':id'=>$id));
			$delete = pdo_delete($this->tablename_schedule, array('id' => $id));
			if($delete) {
				$insert_record_array['type'] = 3;
				$insert_record_array['actitle'] = $title;
				pdo_insert($this->tablename_record, $insert_record_array);
				message('数据删除成功.',$this->createWebUrl('Acmanage'),'success');
			} else {
				message('数据删除失败.',$this->createWebUrl('Acmanage'),'error');
			}
		}else if ($op == 'record') {
            $pindex = max(1, intval($_GPC['page']));
            $psize = 20;
            $list = pdo_fetchall("SELECT * FROM " . tablename($this->tablename_record) . " ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize, array());
            $total = pdo_fetchcolumn("SELECT count(*) FROM " . tablename($this->tablename_record) . " ORDER BY id DESC ", array());
            $pager = pagination($total, $pindex, $psize);
        } else if($op == 'list') {

            $where = "";
            $l_birth            = $_GPC['l_birth'];
            $l_title            = $_GPC['l_title'];
            $l_type             = $_GPC['l_type'];
            $l_content_people   = $_GPC['l_content_people'];
            $l_business_people  = $_GPC['l_business_people'];

            if($l_birth) {
                if($l_birth['start'] != $l_birth['end'])
                {
                    $start  = strtotime($l_birth['start']);
                    $end    = strtotime($l_birth['end'])+86399;
                    $where.=" and actime >= ".$start." and actime <=".$end;
                }
            }
            if($l_title) {
                $where.=" and title like '%".$l_title."%'";
            }

            if($l_type) {
                $where.=" and type = '".$l_type."'";
            }

            if($l_content_people) {
                $where.=" and content_people like '%".$l_content_people."%'";
            }

            if($l_business_people) {
                $where.=" and business_people like '%".$l_business_people."%'";
            }
            $pindex = max(1, intval($_GPC['page']));
            $psize = 20;
            $list = pdo_fetchall("SELECT * FROM " . tablename($this->tablename_schedule)." where 1=1 " .$where. " ORDER BY actime DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize, array());
            $total = pdo_fetchcolumn("SELECT count(*) FROM " . tablename($this->tablename_schedule) ."where 1=1 " .$where. " ORDER BY actime DESC ", array());
            $pager = pagination($total, $pindex, $psize);

		} else {
			
			$last_one_time = pdo_fetchcolumn("SELECT actime FROM " . tablename($this->tablename_schedule) . " order by actime desc limit 1", array());
			
			$today = date("Y-m-d");
			$today_time = strtotime($today);

            $w = date("w");
            $astime = $w*86400;

			$pai = ceil(($last_one_time-$today_time+$astime)/604800);
			$day = ceil(($last_one_time-$today_time+$astime)/86400);

			if($pai<4){
				$pai=4;
			}
			if($day<28){
				$day=28;
			} else {
				$yushu = ceil($day%7);
				$day = (7-$yushu)+$day;		
			}
			$birth = $_GPC['birth'];
			//显示用
			$weekarray_show=array("一","二","三","四","五","六","日");
			$w = date("w");
			if($w=='0'){
				$w=7;
			}
			$startday =date("Y-m-d",strtotime(1-$w." days"));
			$starttime = strtotime($startday);
			$endtime = $starttime+86400*7*$pai-1;
			$endday = date("Y-m-d",$endtime);

			if($birth){
				$start_day	= $birth['start'];
				$start_time	= strtotime($start_day);
				$w = date("w",$start_time);
				if($w=='0'){
					$w=7;
				}
				$starttime = $start_time-86400*($w-1);
				$startday	= date("Y-m-d",$starttime);

				$endday		= $birth['end'];
				$endtime	= strtotime($endday);

				$pai = ceil(($endtime-$starttime)/604800);
				$day = ceil(($endtime-$starttime)/86400);
				
				if($endtime == $starttime) {
					$pai=1;
				}
			}

			$schedule = pdo_fetchall("SELECT * FROM " . tablename($this->tablename_schedule) . " WHERE actime >= :actime", array(':actime' => $starttime));
			for($start = $starttime; $start <= $endtime;$start += 86400) {

		        $acday[] =  date('m-d',$start);
				$actime[] =$start;
			}

		    foreach($schedule as $k=>$v) {

		    	for($t=0;$t<$day;$t++){
			    	if($v['actime'] == $actime[$t]){
			    		$acrows[$t][$v[rank]] = $v['title'];
			    		$actypes[$t][$v[rank]] = $v['type'];
                        $swdj[$t][$v[rank]] = $v['business_people'];
                        $nrdj[$t][$v[rank]] = $v['content_people'];
                        $red[$t][$v[rank]] = $v['status'];
			    	}
		    	}
		    }
		}
		include $this->template("auction");
	}

}